<?php

  include_once("memcache.class.php");
  include_once("log.class.php");

  class databaseAPI {
    
    /************************/
    /** Database information **/
    /************************/
    var $dbhost = "localhost";
    var $dbuser = "root";
    var $dbpass = "";
    var $dbname = "gtalkbots";
    var $db = NULL;
    
    /******************************/
    // CONSTRUCTOR DEFINITION //
    /******************************/
    function __construct() {
      $this->connect();
    }
    
    /*************************************************/
    // Function establishes a connection to database //
    /*************************************************/
    function connect() {
      // Connect to the dbhost
      $connection = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die(mysql_error());
	  
	  // If connection fails send a mail to $dbmail about the same
      if(!$connection) {
        echo "Failed to establish a connection to host";
        exit;
      }
      else {
        // Connect to dbname
        $database = @mysql_select_db($this->dbname);
		
		// If fails to connect to the database send a mail to $dbmail
        if(!$database) {
          echo "Failed to establish a connection to database";
          exit;
        }
        else {
          $this->db = $connection;
        }
      }
    }
    
    /*******************************************/
    // Function closes the database connection //
    /*******************************************/
    function close() {
      mysql_close($this->db);
    }
    
    /*****************************************************************************/
    // Function executes the query against database and returns the result set   //
    // Result returned is in associative array format, and then frees the result //
    /*****************************************************************************/
    function getData($query,$options=array("type"=>"array","cache"=>"on"),$resultset="") {
	  // Lookup on memcache servers if cache is on
	  if($options['cache'] == "on") {
	    $obj = new memcacheAPI();
		if($obj->connect()) {
		  // Try to fetch from memcache server if present
		  $resultset = $obj->getCache($query);
		}
		else {
		  // Fetch query from the database directly
		}
	  }
	  // If $resultset == "" i.e. either caching is off or memcache server is down || $resultset == null i.e. when $query is not cached
	  if($resultset == "" || $resultset == null) {
		$result = mysql_query($query,$this->db);	  
		if($result) {
		  if($options['type'] == "original") {
		    // Return the original result set, if passed options request for the same
			$resultset = $result;
		  }
		  else if($options['type'] == "array") {
			// Return the associative array and number of rows
			$mysql_num_rows = mysql_num_rows($result);
			$result_arr = array();
			while($info = mysql_fetch_assoc($result)) {
			  array_push($result_arr,$info);
			}
			$resultset = array("mysql_num_rows" => $mysql_num_rows,"result" => $result_arr,"false_query" => "no");
		  }
		  // Cache the $query and $resultset
		  $obj->setCache($query,$resultset);
		  $obj->close();
		  return $resultset;
		  // Free the memory
		  mysql_free_result($result);
        }
        else {
		  $resultset = array("false_query" => "yes");
		  return $resultset;
        }
	  }
	  else {
	    // If $query was found in the cache, simple return it
		$obj->close();
	    return $resultset;
	  }
    }
  
    /*************************************************************************/
    // Function executes the query against database (INSERT, UPDATE) types   //
    /*************************************************************************/
    function setData($query) {
      // Run the query
      $result = mysql_query($query,$this->db);
      // Return the result
      return array('result'=>$result,'mysql_affected_rows'=>mysql_affected_rows());
    }
	
  }
?>
